package com.jz.opensl;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private int INIT = 0;
    private int RECORDING = 1;
    private int PLAYING = 2;

    TextView display;

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        display =  findViewById(R.id.show);
        findViewById(R.id.startr).setOnClickListener(this);
        findViewById(R.id.endr).setOnClickListener(this);
        findViewById(R.id.startp).setOnClickListener(this);
        findViewById(R.id.stopp).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startr:
                if(getState() != 0){
                    Toast.makeText(this, "音频设备正在工作中", Toast.LENGTH_SHORT).show();
                    return;
                }
                display.setText("录音中...");
                startRecord();
                break;

            case R.id.endr:
                display.setText("停止录音");
                stopRecorder();
                break;

            case R.id.startp:
                if(getState() != 0){
                    Toast.makeText(this, "音频设备正在工作中", Toast.LENGTH_SHORT).show();
                    return;
                }
                display.setText("播放中...");
                startPlay();
                break;

            case R.id.stopp:
                display.setText("停止播放");
                stopPlay();
                break;

        }
    }


    private void startRecord(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                startRecorder();
            }
        }).start();
    }


    private void startPlay(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                startplay();
            }
        }).start();
    }



    public native int getState();

    public native void startRecorder();

    public native void stopRecorder();

    public native void startplay();

    public native void stopPlay();
}
